package com.badshun.tedrahero;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.Gdx2DPixmap;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.Shape;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badshun.box2d.Box2DWorld;
import com.badshun.enemy.BoneEnemy;
import com.badshun.map.GameMap;
import com.badshun.player.MainPlayer;
import com.badshun.ui.GameUI;

public class MyGdxGame extends ApplicationAdapter {
	private Stage uiStage;
	private GameUI gameUI;
	private OrthographicCamera gameCamera;
	private GameMap gameMap;
	private Box2DWorld b2dWorld;
	private MainPlayer mainPlayer;
	private BoneEnemy boneEnemy;
	
	@Override
	public void create() {
		gameCamera = new OrthographicCamera();
		gameCamera.setToOrtho(false, Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2);
		
		b2dWorld = new Box2DWorld(gameCamera);

		gameMap = new GameMap(gameCamera, b2dWorld);
		
		BodyDef bDef = new BodyDef();
		bDef.type = BodyDef.BodyType.StaticBody;
		bDef.position.set(-10, gameMap.getMapHeight() / 2);
		
		PolygonShape shape = new PolygonShape();
		shape.setAsBox(20, gameMap.getMapHeight() / 2);
		
		FixtureDef fDef = new FixtureDef();
		fDef.shape = shape;
		fDef.filter.categoryBits = 4;
		fDef.filter.maskBits = 2 | 8;
		
		b2dWorld.world.createBody(bDef).createFixture(fDef);
		
		bDef.position.set(gameMap.getMapWidth() / 2, gameMap.getMapHeight() + 20);
		
		shape.setAsBox(gameMap.getMapWidth() / 2, 20);
		
		fDef.shape = shape;
		fDef.filter.categoryBits = 4;
		fDef.filter.maskBits = 2 | 8;
		
		b2dWorld.world.createBody(bDef).createFixture(fDef);
		
		bDef.position.set(gameMap.getMapWidth() + 10, gameMap.getMapHeight() / 2);
		
		shape.setAsBox(20, gameMap.getMapHeight() / 2);
		
		fDef.shape = shape;
		fDef.filter.categoryBits = 4;
		fDef.filter.maskBits = 2 | 8;
		
		b2dWorld.world.createBody(bDef).createFixture(fDef);
		
		bDef.position.set(gameMap.getMapWidth() / 2, -10);
		
		shape.setAsBox(gameMap.getMapWidth() / 2, 20);
		
		fDef.shape = shape;
		fDef.filter.categoryBits = 4;
		fDef.filter.maskBits = 2 | 8;
		
		b2dWorld.world.createBody(bDef).createFixture(fDef);
		
		mainPlayer = new MainPlayer(gameCamera, b2dWorld);
		boneEnemy = new BoneEnemy(gameCamera, b2dWorld);
		
		gameUI = new GameUI(mainPlayer);
		uiStage = new Stage();
		uiStage.addActor(gameUI.touchpad);
		uiStage.addActor(gameUI.aBtn);
		
		Gdx.input.setInputProcessor(uiStage);
		
		gameUI.touchpad.setPosition(10, 10);
		gameUI.aBtn.setPosition(Gdx.graphics.getWidth() - gameUI.aBtn.getWidth() - 30, gameUI.touchpad.getCenterY() - gameUI.aBtn.getHeight() / 2);
	}

	@Override
	public void render() {		
		Gdx.gl20.glClearColor(1, 1, 1, 1);
		Gdx.gl20.glClear(Gdx.gl20.GL_COLOR_BUFFER_BIT);
		
		if(mainPlayer.position.x > Gdx.graphics.getWidth() / 4 && mainPlayer.position.x < gameMap.getMapWidth() - Gdx.graphics.getWidth() / 4) {
			gameCamera.position.set(mainPlayer.position.x, gameCamera.position.y,0);
			gameCamera.update();
		}
		
		if(mainPlayer.position.y > Gdx.graphics.getHeight() / 4 && mainPlayer.position.y < gameMap.getMapHeight() - Gdx.graphics.getHeight() / 4) {
			gameCamera.position.set(gameCamera.position.x, mainPlayer.position.y,0);
			gameCamera.update();
		}
		
		gameMap.render();
		
		boneEnemy.render();
		mainPlayer.render();
		
		b2dWorld.render();
		
		uiStage.act();
		uiStage.draw();
		gameUI.render();
	}
}
